package com.example.linktop.fragmenttest;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.linktop.fragmenttest.Fragments.AnotherRightFrg;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private  Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.btn = (Button)findViewById(R.id.leftFrag_btn);
        this.btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.leftFrag_btn) {

            replaceFragment(new AnotherRightFrg());


        }


    }

    private void replaceFragment(Fragment v) {

        if (v != null) {

            //切换fragment
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction trans = manager.beginTransaction();
            trans.replace(R.id.right_layout,v);
            trans.addToBackStack(null);
            trans.commit();
        }

    }
}
